/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DBLayer;
import ModelLayer.*;
import java.sql.*;

/**
 *
 */
public class DBCityZip implements IFDBCityZip {
     private Connection con;
    /** Creates a new instance
     * of DBCustomer */
    public DBCityZip() {
      con = DBConnection.getInstance().getDBcon();
    }

    public CityZip findCityZipByZip(String zipCode) {
	String query = buildQuery("pCode=" + zipCode);
	ResultSet results;
	CityZip zipObj = null;
	try {
	    Statement stmt = con.createStatement();
	    stmt.setQueryTimeout(5);
	    results = stmt.executeQuery(query);
	    if (results.next()) {
		zipObj = buildCityZip(results);
	    }
	    stmt.close();
	}
	catch (Exception e) {
	    System.out.println("Query exception: "+e.getMessage());
	}
	return zipObj;
    }

    private String buildQuery(String wClause)
    {
	String query="Select * from CityZip";

	    if (wClause.length()>0)
		    query=query+" WHERE "+ wClause;

	    return query;
    }
    
    private CityZip buildCityZip(ResultSet results)
    {   CityZip zipObj = new CityZip();
	try{
	    zipObj.setId(results.getInt(1));
	    zipObj.setpCode(results.getString(2));
	    zipObj.setCity(results.getString(3));
	}
	catch(Exception e)
	{
	    System.out.println("error in building the cityzip object");
	}
	return zipObj;
    }
}
